﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CommonLib.Pattern.Pool
{
	public class ObjectPool : LazySingleton<ObjectPool>, IObjectPool
	{
		protected Dictionary<Type, SubPool> Pools;
		protected Transform Container;
		protected ObjectPool()
		{
			Pools = new Dictionary<Type, SubPool>();
			Container = new GameObject("ObjectPool").transform;
		}

		public T AcquireObject<T>() where T : ReusableObject
		{
			if (!Pools.TryGetValue(typeof(T), out SubPool subPool))
			{
				SubPool newPool = new SubPool(Container, typeof(T).ToString());
				Pools.Add(typeof(T), newPool);
				subPool = newPool;
			}
			return subPool.AcquireObject<T>();
		}

		//return default;


		public void RecycleObject(ReusableObject unUseObject)
		{
			if (!Pools.TryGetValue(unUseObject.GetType(), out SubPool subPool))
			{
				SubPool newPool = new SubPool(Container, unUseObject.GetType().ToString());
				Pools.Add(unUseObject.GetType(), newPool);
				subPool = newPool;
			}
			subPool.RecycleObject(unUseObject);
		}
	}
}